//
//  ChanepasswordViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ChanepasswordViewController.h"

@interface ChanepasswordViewController ()
@property (nonatomic, strong) UITextField *oldPassWordField;//原密码
@property (nonatomic, strong) UITextField *surepassWordField;//确认密码
@property (nonatomic, strong) UITextField *newpassWordField;//新密码

@end

@implementation ChanepasswordViewController

- (void)changePassWord {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:CHANGEPASSWORD parameters:@{@"action":@"modifyPassword",@"newPassword":self.newpassWordField.text , @"password":[userManager getUsrPassWord] , @"userId":[userManager getUsrID]} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSLog(@"aaaaa%@", dic[@"msg"]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"11111%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"修改密码";
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    NSArray *array = @[@"原密码", @"新密码", @"确认密码"];
    
    for (int i = 0; i < 3; i ++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + (70 * i), kWidth - 20, 50)];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 5;
        view.layer.borderWidth = 0.5;
        //创建前面的label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 30)];
        label.text = array[i];
        [view addSubview:label];
        
      
        [self.view addSubview:view];
        
    }
    
    self.oldPassWordField = [[UITextField alloc] initWithFrame:CGRectMake(100, 20, 200, 30)];
    [self.view addSubview:self.oldPassWordField];
    
    self.newpassWordField = [[UITextField alloc] initWithFrame:CGRectMake(100, 90, 200, 30)];
    [self.view addSubview:self.newpassWordField];
    
    self.surepassWordField = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 200, 30)];
    [self.view addSubview:self.surepassWordField];
    
    
    //提交按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 260, kWidth - 20, 50);
    [button setTitle:@"提交" forState:UIControlStateNormal];
    button.block = ^{
        [self submit];//提交按钮
    };
    button.layer.cornerRadius = 5;
    button.layer.borderWidth = 0.5;
    button.backgroundColor = [UIColor orangeColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:button];
    
 
    // Do any additional setup after loading the view from its nib.
}

- (void) submit {

    //判断写的原密码是不是和自己的密码匹配
    if ([self.oldPassWordField.text isEqualToString:[userManager getUsrPassWord]]) {
        if ([self.newpassWordField.text isEqualToString:self.surepassWordField.text]) {
            [self changePassWord];
        }else{
            UIAlertController *controllerVC = [UIAlertController alertControllerWithTitle:@"确认密码不一致" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [controllerVC addAction:[UIAlertAction actionWithTitle:@"晓得了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [controllerVC dismissViewControllerAnimated:YES completion:nil];
            }]];
            [self presentViewController:controllerVC animated:YES completion:nil];
        }
    }else{
        UIAlertController *conVC = [UIAlertController alertControllerWithTitle:@"原密码输入错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [conVC addAction:[UIAlertAction actionWithTitle:@"重新输入" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [conVC dismissViewControllerAnimated:YES completion:nil];
        }]];
        [self presentViewController:conVC animated:YES completion:nil];
    }
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
